/**
 * 
 */
package registnet.courseDemand;

import java.util.List;

/**
 * A utility class for the schedule of classes. Contains methods for determining
 * the section names, days and hours in string format
 * 
 * @author Dexter Ryan Carrasco
 */
public class ScheduleUtility implements CourseDemandConstants {

	/**
	 * Get the char day equivalent of the specified day. This format is for two
	 * or more days schedule
	 * 
	 * @param day
	 * @return letter
	 */
	private static String getDayChar(final int day) {
		String dayString;

		switch (day) {
			case MONDAY:
				dayString = "M";
				break;
			case TUESDAY:
				dayString = "T";
				break;
			case WEDNESDAY:
				dayString = "W";
				break;
			case THURSDAY:
				dayString = "Th";
				break;
			case FRIDAY:
				dayString = "F";
				break;
			case SATURDAY:
				dayString = "S";
				break;
			default:
				dayString = "TBA";
				break;
		}

		return dayString;
	}

	/**
	 * Gets the string days equivalent of the specified days
	 * 
	 * @param daysInt
	 * @return string days equivalent
	 */
	public static String getDaysStringEquivalent(final List<Integer> daysInt) {
		if (daysInt == null) {
			return "TBA";
		}

		final int numOfDays = daysInt.size();
		final StringBuffer sb = new StringBuffer();

		switch (numOfDays) {
			case 1:
				sb.append(getDayString(daysInt.get(0)));
				break;
			case 2:
				sb.append(getDayChar(daysInt.get(0)));
				sb.append(getDayChar(daysInt.get(1)));
				break;
			case 3:
				final int day1 = daysInt.get(0);
				final int day2 = daysInt.get(1);
				final int day3 = daysInt.get(2);

				if (day2 == day1 + 1 && day2 == day3 - 1) {
					sb.append(getDayChar(day1));
					sb.append('-');
					sb.append(getDayChar(day3));
				} else {
					sb.append(getDayChar(day1));
					sb.append(getDayChar(day2));
					sb.append(getDayChar(day3));
				}
				break;
			case 4:
				sb.append(getDayChar(daysInt.get(0)));
				sb.append('-');
				sb.append(getDayChar(daysInt.get(3)));
				break;
			default:
				sb.append("TBA");
				break;
		}

		return sb.toString();
	}

	/**
	 * Get the string day equivalent of the specified day. This format is for
	 * one day schedule
	 * 
	 * @param day
	 * @return string day
	 */
	private static String getDayString(final int day) {
		String dayString;

		switch (day) {
			case MONDAY:
				dayString = "Mon";
				break;
			case TUESDAY:
				dayString = "Tue";
				break;
			case WEDNESDAY:
				dayString = "Wed";
				break;
			case THURSDAY:
				dayString = "Thu";
				break;
			case FRIDAY:
				dayString = "Fri";
				break;
			case SATURDAY:
				dayString = "Sat";
				break;
			default:
				dayString = "TBA";
				break;
		}

		return dayString;
	}

	/**
	 * Gets the letter equivalent of the specified time
	 * 
	 * @param timeInt
	 * @return letter
	 */
	private static char[] getLetterEquivalent(final int timeInt) {
		final char[] value = new char[2];

		switch (timeInt) {
			case 0:
			case 1:
			case 2:
				value[0] = 'A';
				value[1] = 'S';
				break;
			case 3:
			case 4:
			case 5:
				value[0] = 'B';
				value[1] = 'T';
				break;
			case 6:
			case 7:
			case 8:
				value[0] = 'C';
				value[1] = 'U';
				break;
			case 9:
			case 10:
			case 11:
				value[0] = 'D';
				value[1] = 'V';
				break;
			case 12:
			case 13:
			case 14:
				value[0] = 'E';
				value[1] = 'W';
				break;
			case 15:
			case 16:
			case 17:
				value[0] = 'F';
				value[1] = 'X';
				break;
			case 18:
			case 19:
			case 20:
				value[0] = 'G';
				value[1] = 'Y';
				break;
			case 21:
			case 22:
			case 23:
				value[0] = 'H';
				value[1] = 'Z';
				break;
			default:
				value[0] = 'H';
				value[1] = 'Z';
				break;
		}

		return value;
	}

	/**
	 * Gets the section name equivalent of the specified time and days
	 * 
	 * @param timeInt
	 * @param daysInt
	 * @return section name
	 */
	public static String getSectionName(final int[] timeInt,
			final List<Integer> daysInt) {

		if (timeInt == null || daysInt == null) {
			return "TBA";
		}

		final StringBuffer sb = new StringBuffer();
		final char[] value1 = getLetterEquivalent(timeInt[0]);
		final char[] value2 = getLetterEquivalent(timeInt[1]);
		final int day = daysInt.get(0);

		if (day == MONDAY || day == WEDNESDAY || day == FRIDAY) {
			sb.append(value1[0]);

			if (!(value1[0] == value2[0])) {
				sb.append(value2[0]);
			}
		} else {
			sb.append(value1[1]);
			if (!(value1[1] == value2[1])) {
				sb.append(value2[1]);
			}
		}

		return sb.toString();
	}

	/**
	 * Gets the string time equivalent of the specified time
	 * 
	 * @param timeInt
	 * @return string time equivalent
	 */
	public static String getTimeStringEquivalent(final int timeInt[]) {
		if (timeInt == null) {
			return "TBA";
		}

		final StringBuffer sb = new StringBuffer();

		switch (timeInt[0]) {
			case 0:
				sb.append("7");
				break;
			case 1:
				sb.append("7:30");
				break;
			case 2:
				sb.append("8");
				break;
			case 3:
				sb.append("8:30");
				break;
			case 4:
				sb.append("9");
				break;
			case 5:
				sb.append("9:30");
				break;
			case 6:
				sb.append("10");
				break;
			case 7:
				sb.append("10:30");
				break;
			case 8:
				sb.append("11");
				break;
			case 9:
				sb.append("11:30");
				break;
			case 10:
				sb.append("12");
				break;
			case 11:
				sb.append("12:30");
				break;
			case 12:
				sb.append("1");
				break;
			case 13:
				sb.append("1:30");
				break;
			case 14:
				sb.append("2");
				break;
			case 15:
				sb.append("2:30");
				break;
			case 16:
				sb.append("3");
				break;
			case 17:
				sb.append("3:30");
				break;
			case 18:
				sb.append("4");
				break;
			case 19:
				sb.append("4:30");
				break;
			case 20:
				sb.append("5");
				break;
			case 21:
				sb.append("5:30");
				break;
			case 22:
				sb.append("6");
				break;
			case 23:
				sb.append("6:30");
				break;
			default:
				sb.append("TBA");
				break;
		}

		sb.append('-');

		switch (timeInt[1]) {
			case 0:
				sb.append("7:30");
				break;
			case 1:
				sb.append("8");
				break;
			case 2:
				sb.append("8:30");
				break;
			case 3:
				sb.append("9");
				break;
			case 4:
				sb.append("9:30");
				break;
			case 5:
				sb.append("10");
				break;
			case 6:
				sb.append("10:30");
				break;
			case 7:
				sb.append("11");
				break;
			case 8:
				sb.append("11:30");
				break;
			case 9:
				sb.append("12");
				break;
			case 10:
				sb.append("12:30");
				break;
			case 11:
				sb.append("1");
				break;
			case 12:
				sb.append("1:30");
				break;
			case 13:
				sb.append("2");
				break;
			case 14:
				sb.append("2:30");
				break;
			case 15:
				sb.append("3");
				break;
			case 16:
				sb.append("3:30");
				break;
			case 17:
				sb.append("4");
				break;
			case 18:
				sb.append("4:30");
				break;
			case 19:
				sb.append("5");
				break;
			case 20:
				sb.append("5:30");
				break;
			case 21:
				sb.append("6");
				break;
			case 22:
				sb.append("6:30");
				break;
			case 23:
				sb.append("7");
				break;
			default:
				sb.append("TBA");
				break;
		}

		return sb.toString();
	}
}
